import { LOGIN_TOKEN } from '../actionTypes'
import { login } from '@/api'
import { setToken } from '@/utils'
import { message } from 'antd'

// 包裹异步的操作
export function loginAction ({ mobile, code }) {
  return async dispatch => {
    // 异步逻辑搞定之后，触发dispatch
    const res = await login({ mobile, code })
    // 保存token
    setToken(res.data.token)
    message.success('登录成功！')

    dispatch({
      type: LOGIN_TOKEN,
      // 保存的token
      payload: res.data.token
    })
  }
}
